package constant.milk.toilet.dialog;

import constant.milk.toilet.R;
import constant.milk.toilet.util.MilkUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MilkDialog extends Dialog
{
	private Context context;
	private View contentView;
	private TextView titleView;
	private TextView leftButton;
	private TextView rightButton;
	private View.OnClickListener leftClickListener;
	private View.OnClickListener rightClickListener;
	
	private String title;
	private String leftTitle;
	private String rightTitle;
	
	public float TEXT_SIZE;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_two);
		
		setSettingFontSize();
		
		contentView = findViewById(R.id.dialogTwoContentView);
		titleView = (TextView) findViewById(R.id.dialogTitleTextView);
		
		leftButton = (TextView) findViewById(R.id.dialogLeftTextView);
		rightButton = (TextView) findViewById(R.id.dialogRightTextView);
		
		setTitle(title);
		setClickListener(leftClickListener, rightClickListener);
		
		contentView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});
	}

	public MilkDialog(Context context, String title,
			View.OnClickListener leftListener,
			View.OnClickListener rightListener)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		this.context = context;
		this.leftClickListener = leftListener;
		this.rightClickListener = rightListener;
		this.title = title;
		this.leftTitle = null;
		this.rightTitle = null;
	}
	
	public void setLeftTitle(String title)
	{
		this.leftTitle = title;
	}
	
	public void setRightTitle(String title)
	{
		this.rightTitle = title;
	}

	private void setTitle(String title)
	{
		titleView.setTextColor(Color.BLACK);
		titleView.setTextSize(TEXT_SIZE);
		titleView.setText(title);
	}
	
	private void setClickListener(View.OnClickListener left , View.OnClickListener right)
	{        
		leftButton.setTextColor(Color.WHITE);
		leftButton.setTextSize(TEXT_SIZE);
		
		if (leftTitle != null)
		{
			leftButton.setText(leftTitle);
		}
		
		rightButton.setTextColor(Color.WHITE);
		rightButton.setTextSize(TEXT_SIZE);
		
		if (rightTitle != null)
		{
			rightButton.setText(rightTitle);
		}
		
		if(left != null && right != null)
		{            
			leftButton.setOnClickListener(left);            
			rightButton.setOnClickListener(right);        
		}
		else if (left != null && right == null)
		{            
			leftButton.setOnClickListener(left);   
			rightButton.setVisibility(View.GONE);
		}
		else if (right != null && left == null)
		{            
			rightButton.setOnClickListener(left);   
			leftButton.setVisibility(View.GONE);
		}
		else 
		{                     
			rightButton.setVisibility(View.GONE); 
			leftButton.setVisibility(View.GONE);
		}    
	}
	
	private void setSettingFontSize()
	{
		int fontSize = 9;
		
		SharedPreferences p = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = p.getInt(context.getString(R.string.prefFont), 1);
		
		if (fontFlag == 0) fontSize = 13;
		else if (fontFlag == 1) fontSize = 16;
		else if (fontFlag == 2) fontSize = 20;
		
		TEXT_SIZE = fontSize;
	}
}
